/*
 * @(#)IntegrateConnection.java    �汾�� 2010-11-11
 * 
 * Version information
 * 
 * Copyright notice
 */

package cn.edu.nju.software.localserver;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import cn.edu.nju.software.XML.XMLParser;
import cn.edu.nju.software.XML.XMLProducer;
import cn.edu.nju.software.XML.XMLValidator;
import cn.edu.nju.software.beans.CourseBean;

/**
 * Class description goes here.
 * 
 * @version �汾�� 2010-11-11
 * @author lijinsheng
 */
public class SendToIntegrateServer {
	/* This class interact with the integration server */
	private DataOutputStream outputStream = null;
	private DataInputStream inputStream = null;

	public SendToIntegrateServer() {
		final String ip = "127.0.0.1";
		final int port = 8700;
		Socket socket = null;
		try {
			socket = new Socket(ip, port);
			outputStream = new DataOutputStream(new BufferedOutputStream(
					socket.getOutputStream()));
			inputStream = new DataInputStream(new BufferedInputStream(
					socket.getInputStream()));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// send the share requeste to the integrate server and get the xmlFile from
	// the integrate server and parse the xmlfile
	public ArrayList<CourseBean> sendShareCourseInformation() {
		try {
			outputStream.writeInt(1);
			outputStream.writeUTF("A");
			outputStream.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// local file to store the xml file from the integrate server

		File xmlFile1 = new File("E:\\oneShareCourse.xml");
		FileHandler.readFile(xmlFile1, inputStream);

		ArrayList<CourseBean> courses = new ArrayList<CourseBean>();
		if (XMLValidator.validate(xmlFile1, new File(
				"xmlFiles/ACourseSchema.xsd"))) {
			courses.addAll(XMLParser.parseCourse(xmlFile1));
		}

		return courses;
	}

	// send the select outer institute course(Using xml file format) and get a
	// boolean value from the integrate server
	public boolean toIntegrateSelectCourseInformation(
			ArrayList<StudentCourseBean> studentCourseBeans) {
		if(studentCourseBeans.size() == 0) {
			return true;
		}

		try {
			outputStream.writeInt(2);
			outputStream.writeUTF("A");
			outputStream.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File xmlFile = XMLProducer.produceStudentCourseFile(studentCourseBeans);
		FileHandler.writeFile(xmlFile, outputStream);
		try {
			outputStream.close();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		try {
			inputStream.close();
			outputStream.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return true;
	}

	// send the unSelect outer institute course(Using xml file format) and get a
	// boolean value from the integrate server
	public boolean toIntegrateUnSelectCourseInformation(
			ArrayList<StudentCourseBean> studentCourseBeans) {
		if(studentCourseBeans.size()== 0) {
			return true;
		}
		
		try {
			outputStream.writeInt(3);
			outputStream.writeUTF("A");
			outputStream.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File xmlFile = XMLProducer.produceStudentCourseFile(studentCourseBeans);
		FileHandler.writeFile(xmlFile, outputStream);

		try {
			inputStream.close();
			outputStream.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		return true;
	}

	// send the course delete information(using xml file format) to the
	// integrate server and get a boolean value from the integrate server
	public boolean toIntegrateDeleteCourseInformation(ArrayList<String> cids) {
		try {
			outputStream.writeInt(4);
			outputStream.writeUTF("A");
			outputStream.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File xmlFile = XMLProducer.produceCourseIDFile(cids);
		FileHandler.writeFile(xmlFile, outputStream);
		try {
			inputStream.close();
			outputStream.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return true;
	}

	// send the student delete information(using xml file format) to the
	// integrate server and get a boolean value from the integrate server
	public boolean toIntegrateDeleteStudentInformation(ArrayList<String> sids) {
		try {
			outputStream.writeInt(5);
			outputStream.writeUTF("A");
			outputStream.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File xmlFile = XMLProducer.produceStudentIDFile(sids);
		FileHandler.writeFile(xmlFile, outputStream);

		try {
			inputStream.close();
			outputStream.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return true;
	}
}
